package com.asman.vr;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.asman.vr.dto.SchemeInfoDetailData;
import com.asman.vr.network.TrtcAuthInfoData;
import com.google.gson.Gson;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

import static io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import static io.flutter.plugin.common.MethodChannel.Result;

public class FlutterVrWebView implements PlatformView, MethodCallHandler  {

    // private final TextView textView;

    private final VRWebView vrWebView;
    private final MethodChannel methodChannel;

    FlutterVrWebView(Context context, Activity activity, BinaryMessenger messenger, int id, Map<String, Object> params) {
        methodChannel = new MethodChannel(messenger, "plugins.com.asman/vr_" + id);
        methodChannel.setMethodCallHandler(this);

        String url = (String) params.get("urlPath");

        Boolean hiddenTitle = true;
        Object hiddenTitleParam = params.get("hiddenTitle");
        if (hiddenTitleParam != null) {
            hiddenTitle = (int) hiddenTitleParam == 1;
        }

        Boolean isFromReceive = false;
        Object isFromReceiveParam = params.get("isFromReceive");
        if (isFromReceiveParam != null) {
            isFromReceive = (int) isFromReceiveParam == 1;
        }

        Boolean fullScreen = false;
        Object fullScreenParam = params.get("fullScreen");
        if (fullScreenParam != null) {
            fullScreen = (int) fullScreenParam == 1;
        }

        // Boolean fullScreen = (int) params.get("fullScreen") == 1;
        // Boolean isFromReceive = (int) params.get("isFromReceive") == 1;
        if (hiddenTitle == null) hiddenTitle = true;
        fullScreen = false;
        // if (fullScreen == null) fullScreen = true;
        if (isFromReceive == null) isFromReceive = false;

        String tenantId = "0";
        Object tenantIdO = params.get("tenantId");
        if (tenantIdO != null) {
            tenantId = String.valueOf(tenantIdO);
        }
        String shareBaseUrl = (String) params.get("shareBaseUrl");
        vrWebView = new VRWebView(context, url, methodChannel, hiddenTitle, fullScreen, isFromReceive, tenantId, shareBaseUrl);

        /*textView = new TextView(context);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodChannel.invokeMethod("flutterMethod", "这是原生调 flutter 传的参数");
            }
        });*/
    }

    @Override
    public View getView() {
        // return textView;
        return vrWebView;
    }

    @Override
    public void onMethodCall(MethodCall methodCall, Result result) {
        switch (methodCall.method) {
            /*case "setText":
                setText(methodCall, result);
                break;*/
            case "joinRoom":
                String trtcAuthInfoDataJson = (String) methodCall.arguments;
                Gson gson = new Gson();
                TrtcAuthInfoData trtcAuthInfoData = gson.fromJson(trtcAuthInfoDataJson, TrtcAuthInfoData.class);
                this.vrWebView.joinRoom(trtcAuthInfoData);
                break;
            case "requestAudioSuccessCb":
                this.vrWebView.getAudioPermissionCallback().requestAudioSuccessCb();
                break;
            case "requestAudioFailCb":
                this.vrWebView.getAudioPermissionCallback().requestAudioFailCb();
                break;
            case "onBackPressed":
                this.vrWebView.onBackPressed();
                break;
            case "trtcExitRoom":
                this.vrWebView.trtcExitRoom();
                break;
            case "shoreToWX":
                String base64Data = (String) methodCall.arguments;
                this.vrWebView.shoreToWX(base64Data);
                break;
            default:
                result.notImplemented();
        }

    }

    /*private void setText(MethodCall methodCall, Result result) {
        String text = (String) methodCall.arguments;
        textView.setText(text);
        result.success("这是执行原生方法后异步的返回值");
    }*/

    @Override
    public void dispose() {}
}
